package cn.glc.daily.life.infrastructure.repository.redis;

import cn.glc.daily.life.model.ro.UserTaskRo;
import cn.simon.free.redis.component.RedisSupport;
import cn.simon.free.redis.crud.service.RedisCrudService;
import com.alibaba.fastjson.JSON;
import com.google.common.base.MoreObjects;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.concurrent.TimeUnit;

/**
 * @author simon
 * @date 2021/6/1 14:48
 * @copyright:
 * @modifier
 * @motto a no talent, people who have to study hard
 */
@Slf4j
@Repository
public class RedisUserTaskRepository extends RedisCrudService<UserTaskRo, Long> {

    public void saveWithExpire(UserTaskRo userTaskRo) {
        super.save(userTaskRo);
        if (MoreObjects.firstNonNull(userTaskRo.getExpireTime(), 0L) > 0) {
            log.info("用户任务|新增定时推送:{}", JSON.toJSONString(userTaskRo));
            RedisSupport.jedisStringSupport.setExpire(hashKey(userTaskRo.getId()), userTaskRo.getExpireTime(), TimeUnit.MILLISECONDS);
        }
    }
}
